﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MOJsdk;
using System.Windows;
using System.Windows.Data;
using System.Globalization;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace MojAddNewMovie
{
    public class UserVars
    {
        public static string ToolButtonID
        { get { return "blkx_addNewMovie_ToolButton"; } }
        public static MojEnumVar<bool> IsWinVisible
        { get { return new MojEnumVar<bool>("blkx_addnewmovie_isSearchMovieWindowVisible", StorageType.StatusVar); } }
    }
    public class AddNewMoviePlugin : IPlugIn
    {
        public string PluginDeveloper
        { get { return "Davidblkx"; } }
        public string PluginVersion
        { get { return "1"; } }
        public string PluginName
        { get { return "MojAddNewMovie"; } }
        public string PluginWeb
        { get { return ""; } }
        public string PluginDescription
        { get { return "Control to add new movies"; } }
        public void Dispose()
        {

        }
        public void Install()
        {

        }
        public void Unistall()
        {

        }

        #region InicializeMethods
        public void Inicialize()
        {
            UserVars.IsWinVisible.Create(false);
            INIToolButton();
        }
        private void INIToolButton()
        {
            MojToolButton btn = new MojToolButton("N", UserVars.ToolButtonID, "Add New Movie", null);
            btn.Click += ToolButtonClicked;
            btn.AddLogicStatment(MojToolButtonShowCondition.IPanel_ManageMovies);
            MojHost.GetHost.ToolButtonCollection.Add(btn);
        }
        #endregion

        #region Events
        private void ToolButtonClicked(object sender, RoutedEventArgs e)
        {
            if (UserVars.IsWinVisible.Value())
                return;

            Control_AddNewMovie myCtrl = new Control_AddNewMovie();
            WindowControl ctrl = MojHost.GetHost.AddNewUserControl(myCtrl, new WindowControlInfo(360, 300) 
                { WindowTitle = MOJLocalization.Localize("Search Movie"), });
            UserVars.IsWinVisible.Set(true);

            ctrl.OnClosed += delegate { UserVars.IsWinVisible.Set(false); };
            myCtrl.onMovieSelected += delegate { ctrl.Close(); };
        }
        #endregion
    }
}

namespace Converters
{
    public class ConvertThumbPathToImage : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            MojWebImage name = value as MojWebImage;
            BitmapImage img;

            if (name == null)
                return new BitmapImage();

            name.DownloadThumbPath();
            img = new BitmapImage(new Uri(name.ThumbPath));

            return img;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
